package cn.com.huak.www.sell.service.impl;

import cn.com.huak.www.sell.dataObject.ProductInfo;
import cn.com.huak.www.sell.dto.CartDTO;
import cn.com.huak.www.sell.enums.ProductStatusEnum;
import cn.com.huak.www.sell.enums.ResultEnum;
import cn.com.huak.www.sell.exception.OrderException;
import cn.com.huak.www.sell.exception.ProductException;
import cn.com.huak.www.sell.repository.ProductInfoRepository;
import cn.com.huak.www.sell.service.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author huak@wanggang
 * @date 2018/12/26 0:04
 */
@Service
public class ProductInfoServiceImpl implements ProductInfoService {

    @Autowired
    private ProductInfoRepository productInfoRepository;

    @Override
    public ProductInfo findOne(String id) {
        return productInfoRepository.findOne(id);
    }

    @Override
    public List<ProductInfo> findUpAll() {
        return productInfoRepository.findByProductStatus(ProductStatusEnum.UP.getCode());
    }

    @Override
    public Page<ProductInfo> findAll(Pageable pageable) {
        return productInfoRepository.findAll(pageable);
    }

    @Override
    public ProductInfo save(ProductInfo productInfo) {
        return productInfoRepository.save(productInfo);
    }

    /* 减少库存 */
    @Override
    @Transactional
    public void decreaseStock(List<CartDTO> cartDTOS) {
        for (CartDTO cartDTO : cartDTOS){
            // 判断当前的商品是否存在
            ProductInfo one = productInfoRepository.findOne(cartDTO.getProductId());
            if(null == one){
                throw new OrderException(ResultEnum.PRODUCT_NOT_EXIST);
            }

            // 判断当前的商品库存是否充足
            int i = one.getProductStock() - cartDTO.getProductQuantity();
            if(i < 0){
                throw new OrderException(ResultEnum.PRODUCT_STOCK_ERROR);
            }

            // 将减少之后的库存保存到数据库
            one.setProductStock(i);
            productInfoRepository.save(one);
        }
    }

    /* 增加库存 */
    @Override
    @Transactional
    public void increaseStock(List<CartDTO> cartDTOS) {
        for (CartDTO cartDTO : cartDTOS){
            // 判断当前的商品是否存在
            ProductInfo one = productInfoRepository.findOne(cartDTO.getProductId());
            if(null == one){
                throw new OrderException(ResultEnum.PRODUCT_NOT_EXIST);
            }

            // 判断当前的商品库存是否充足
            int i = one.getProductStock() + cartDTO.getProductQuantity();

            // 将增加之后的库存保存到数据库
            one.setProductStock(i);
            productInfoRepository.save(one);
        }
    }

    @Override
    public ProductInfo onSale(String productId) {
        //1. 获取当前的商品
        ProductInfo one = productInfoRepository.findOne(productId);
        if(one == null){
            throw new ProductException(ResultEnum.PRODUCT_NOT_EXIST);
        }

        //2. 判断当前商品的状态
        if(one.getProductStatus().equals(ProductStatusEnum.UP.getCode())){
            throw new ProductException(ResultEnum.PRODUCT_STATUS_ERROR);
        }

        //3. 上架
        one.setProductStatus(ProductStatusEnum.UP.getCode());
        return productInfoRepository.save(one);
    }

    @Override
    public ProductInfo offSale(String productId) {
        //1. 获取当前的商品
        ProductInfo one = productInfoRepository.findOne(productId);
        if(one == null){
            throw new ProductException(ResultEnum.PRODUCT_NOT_EXIST);
        }

        //2. 判断当前商品的状态
        if(one.getProductStatus().equals(ProductStatusEnum.DOWN.getCode())){
            throw new ProductException(ResultEnum.PRODUCT_STATUS_ERROR);
        }

        //3. 下架
        one.setProductStatus(ProductStatusEnum.DOWN.getCode());
        return productInfoRepository.save(one);
    }
}
